package com.jvk.check4home;

import android.R.drawable;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.admob.android.ads.AdManager;
import com.admob.android.ads.AdView;
import com.dreasyLib.comm.MessageUtils;
import com.jvk.check4home.HomeAdapter.OnSelectHome;
import com.jvk.check4home.frwk.OutputState;
import com.jvk.check4home.frwk.frwk;
import com.jvk.check4home.frwk.homeDTO;

public class HomeList extends Activity implements OnSelectHome,Runnable {

	/*protected TextView name;
	protected TextView scuarem;
	protected TextView habit;
	protected TextView banios;
	protected TextView precio;
	 *///protected RatingBar puntos;
	protected ImageButton addElement;
	protected ImageButton refresh;
	protected ListView listHomes;
	private ProgressDialog waitingDialog;	
	protected AdView add;

	protected OnClickListener listenerAdd;
	protected OnClickListener listenerRefresh;
	//protected

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.homelist);  
		initXMLElements();
		initListeners();
		AdManager.setTestDevices( new String[] {
				AdManager.TEST_EMULATOR
				} );
		add=(AdView) findViewById(R.id.ad);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu){

		menu.add(0,0,0,"Edit").setIcon(drawable.ic_menu_edit);
		menu.add(0,1,0,"Exit").setIcon(drawable.ic_menu_close_clear_cancel);

		return true;

	}
	public boolean onOptionsItemSelected (MenuItem item){

		switch (item.getItemId()){
		case 0:
			Intent intent = new Intent(HomeList.this, configurationActivity.class);			
			startActivity(intent);
			return true;
		case 1 :
			HomeList.this.finish();
			return true;
		}
		return false;
	}


	private void initListeners() {
		listenerAdd=new OnClickListener() {

			@Override
			public void onClick(View v) {
				addNewHouse();
			}


		};
		listenerRefresh=new OnClickListener() {

			@Override
			public void onClick(View v) {
				refreshHauses();

			}



		};
		addElement.setOnClickListener(listenerAdd);
		refresh.setOnClickListener(listenerRefresh);


	}

	protected void refreshHauses() {
		waitingDialog = ProgressDialog.show(HomeList.this, "", 
				"Loading. Please wait...", true);
		Thread thread = new Thread(this);
		thread.start();

	}

	private void addNewHouse() {
		Intent intent = new Intent(HomeList.this, HomeEdit.class);			
		startActivity(intent);

	}


	private void initXMLElements() {
		addElement=(ImageButton) findViewById(R.id.listhomes_imagebtn_add);
		refresh=(ImageButton) findViewById(R.id.listhomes_imagebtn_reload);
		listHomes=(ListView) findViewById(R.id.listhomes_listview);

	}

	@Override
	protected void onResume() {
		if (frwk.getInstance(getApplicationContext()).reload)refreshHauses();
		super.onResume();
		add.requestFreshAd();

	}

	
	@Override
	protected void onPause() {
		super.onPause();
		frwk.getInstance(getApplicationContext()).reload=false;
	}

	@Override
	public void selectDto(homeDTO pHome) {
		Log.d("gdata","launching ["+pHome.name+"]");
		Intent intent = new Intent(HomeList.this, HomeEdit.class);			
		intent.putExtra("home",pHome);
		startActivity(intent);
	}

	@Override
	public void run() {
		OutputState state=frwk.getInstance(getApplicationContext()).loadBuildingList();
		handler.sendMessage(MessageUtils.OnCompleteMsg(state.detail, state.state));
	}
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			waitingDialog.dismiss();
			int output=msg.getData().getInt(MessageUtils.EVENT);
			switch (output) {
			case 0:
				listHomes.setAdapter(new HomeAdapter(HomeList.this, frwk.getInstance(getApplicationContext()).listBuildings));
				break;
			case -1://null
			Log.d("","");
			AlertDialog.Builder builder = new AlertDialog.Builder(HomeList.this);
			builder.setMessage(getApplicationContext().getString(R.string.dial_user_incorrect)+"[err:-1]")
			.setCancelable(false)
			.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.dismiss();
					Intent intent = new Intent(HomeList.this, configurationActivity.class);			
					startActivity(intent);
				}
			});
			AlertDialog alert = builder.create();
			alert.show();

			break;
			case -2://user
				Log.d("","");
				AlertDialog.Builder builder2 = new AlertDialog.Builder(HomeList.this);
				builder2.setMessage(getApplicationContext().getString(R.string.dial_user_incorrect)+"[err:-2]")
				.setCancelable(false)
				.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.dismiss();
						Intent intent2 = new Intent(HomeList.this, configurationActivity.class);			
						startActivity(intent2);
					}
				});
				AlertDialog alert2 = builder2.create();
				alert2.show();

				break;	
			case -3://unknow
				Log.d("","");
				AlertDialog.Builder builder3 = new AlertDialog.Builder(HomeList.this);
				builder3.setMessage(getApplicationContext().getString(R.string.dial_unknownerror)+"[err:wtf?]")
				.setCancelable(false)
				.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.dismiss();
						Intent intent2 = new Intent(HomeList.this, configurationActivity.class);			
						startActivity(intent2);
					}
				});
				AlertDialog alert3 = builder3.create();
				alert3.show();
				break;	
			default:
				break;
			}
			// todo dependiendo del out debe salir lo que sea
		}
	};	 


}
